<?php


namespace app\common\model;

use think\Model;

class Basemodel extends Model
{

    /**
     * 分页功能
     */
    public function page_index($datas = [], $order = null)
    {
        return $this->where($datas)
            ->order($order)
            ->paginate();
    }


    /**
     * 左边分类
     * @param $data
     * @param $pid
     * @return string
     */
    public function json_right_nav($data, $pid = '0')
    {
        static $arr = array();

        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['url'] = stripos($v['link'], "http") === 0 ? $v['link'] : url($v['link'] . '/index', ['navid'=>$v['id']]);
                $v['chiid'] = $this->json_right_nav($data, $v['id']);
                $arr[] = $v;
            }
        }
        return $arr;
    }


    /**
     * 删除
     * @param $id
     * @return int
     */
    public function del($id)
    {
        return $this->where('id', $id)->delete();
    }

    /**
     * @return 前台无限导航
     */
    public function catenav($data='')
    {

        $list = cache('DB_TREE_ARETYPE');
        if(empty($list)) {
            !empty($data) ? $this->where($data) : '';
            $tptc = $this->order('sort ASC,id ASC')->select();
            $list = $this->_nav($tptc);
            cache('DB_TREE_ARETYPE', $list);
        }
        return $list;

    }

    public function _nav($data, $pid = 0, $level = 0)
    {
        $arr = $html = '';
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['link'] = stripos($v['link'], "http") === 0 ? $v['link'] : url($v['link'] . '/index', ['navid' => $v['id']]);

                $v['chiid']= $this->_nav($data, $v['id'], $level + 1);
                $arr []= $v;

            }
        }
        return  $arr;
    }

    /**
     * @return 无限分类
     */
    public function catetree($data = null, $html = '|----')
    {
        $list= cache('catetree');
        if(empty($list)) {
            !empty($data) ? $this->where($data) : '';
            $tptc = $this->order('sort asc,id ASC')->select();
            $list = $this->sort($tptc, 0, 0, $html);
            cache('catetree',$list);
        }
        return $list;
    }

    public function sort($data, $pid = 0, $level = 0, $html = '|----')
    {
        static $arr = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['level'] = str_repeat($html, $level);
                $arr[] = $v;
                $this->sort($data, $v['id'], $level + 1, $html);
            }
        }
        return $arr;
    }


    /**
     * 排除自己的uid
     */
    public function outpid($pid)
    {
        $re = $this->outsort($this->select(), $pid);
        return $this->sort($this->whereNotIn('id', $re)->select());
    }

    public function outsort($data, $pid = 0)
    {
        static $arr = array();
        $arr[] = $pid;

        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $this->outsort($data, $v['id']);
            }
        }
        return $arr;
    }


    /**
     * 上下页
     * @param $id   当前页面id
     * @param $datas    条件
     */
    public function getnext($id, $where = null)
    {
        $where['id'] = ['<', $id];
        $result ['pre'] = $this->where($where)->order('id desc')->find();
        $result ['pre'] = $result['pre'] ? $result['pre'] : '';

        $where['id'] = ['>', $id];
        $result ['next'] = $this->where($where)->order('id asc')->find();
        $result ['next'] = $result ['next'] ? $result ['next'] : '';

        return $result;
    }

}